package com.example.ceramics.database.dao;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.example.ceramics.database.DatabaseContants;
import com.example.ceramics.database.DatabaseHelper;

public class TableDaoImpl implements ITableDao {

    private final DatabaseHelper databaseHelper;

    public TableDaoImpl(Context context) {
        databaseHelper = new DatabaseHelper(context);
    }

    @Override
    public void createTableUser() {
        SQLiteDatabase db = databaseHelper.getWritableDatabase();
        String sql = "create table if not exists " + DatabaseContants.TABLE_USER +
                "(account varchar primary key," +
                "password varchar," +
                "name varchar," +
                "nickname varchar," +
                "phone varchar," +
                "email varchar," +
                "addressProvince varchar," +
                "addressCity varchar," +
                "addressCounty varchar," +
                "addressDetail varchar," +
                "img varchar)";
        db.execSQL(sql);
        db.close();
    }

    @Override
    public void createTableGoods() {
        SQLiteDatabase db = databaseHelper.getWritableDatabase();
        String sql = "create table if not exists " + DatabaseContants.TABLE_GOODS +
                "(goodsId integer primary key autoincrement," +
                "goodsName varchar," +
                "goodsPrice double," +
                "goodsSignature varchar," +
                "goodsType varchar," +
                "goodsImg integer)";
        db.execSQL(sql);
        db.close();
    }

    @Override
    public void createTableCart() {
        SQLiteDatabase db = databaseHelper.getWritableDatabase();
        String sql = "create table if not exists " + DatabaseContants.TABLE_CART +
                "(account varchar," +
                "goodsId integer," +
                "num integer," +
                "price double," +
                "primary key(account,goodsId))";
        db.execSQL(sql);
        db.close();
    }

    @Override
    public void createTableOrder() {
        SQLiteDatabase db = databaseHelper.getWritableDatabase();
        String sql = "create table if not exists " + DatabaseContants.TABLE_ORDER +
                "(id integer primary key autoincrement," +
                "account varchar," +
                "goodsId integer," +
                "num integer," +
                "price double," +
                "time varchar)";
        db.execSQL(sql);
        db.close();
    }

    @Override
    public void deleteTableUser() {
        SQLiteDatabase db = databaseHelper.getWritableDatabase();
        String sql = "drop table "+DatabaseContants.TABLE_USER;
        db.execSQL(sql);
        db.close();
    }

    @Override
    public void deleteTableGoods() {
        SQLiteDatabase db = databaseHelper.getWritableDatabase();
        String sql = "drop table "+DatabaseContants.TABLE_GOODS;
        db.execSQL(sql);
        db.close();
    }

    @Override
    public void deleteTableCart() {
        SQLiteDatabase db = databaseHelper.getWritableDatabase();
        String sql = "drop table "+DatabaseContants.TABLE_CART;
        db.execSQL(sql);
        db.close();
    }

    @Override
    public void deleteTableOrder() {
        SQLiteDatabase db = databaseHelper.getWritableDatabase();
        String sql = "drop table "+DatabaseContants.TABLE_ORDER;
        db.execSQL(sql);
        db.close();
    }
}
